/*
  文章管理
*/
const express = require('express')
const path = require('path')
const utility = require('utility')
const multer = require('multer')
const upload = multer({dest: 'uploads/'})
const router = express.Router()
const db = require(path.join(__dirname, '../common/db.js'))

// 根据id查询文章的详细信息
router.get('/article/:id', async (req, res) => {
  // 1、获取文章id
  let id = req.params.id
  // 2、根据id查询数据库
  let sql = 'select * from article where id = ?'
  let ret = await db.operateData(sql, id)
  // 3、响应状态
  if (ret && ret.length > 0) {
    res.json({
      status: 0,
      message: '查询文章详情成功',
      data: ret[0]
    })
  } else {
    res.json({
      status: 1,
      message: '查询文章详情失败'
    })
  }
})

// 编辑文章
router.post('/article/edit', upload.single('cover_img'), async (req, res) => {
  // 1、获取文章表单数据
  let params = req.body
  let id = req.user.id
  // 2、把数据插入数据库
  let sql = 'update article set ? where id = ?'
  let obj = {
    title: params.title,
    cate_id: params.cate_id,
    content: params.content,
    // 上传文件成功后，文件在服务器中的路径
    // cover_img: '/uploads/' + req.file.filename,
    state: params.state,
    is_delete: 1,
    pub_date: new Date(),
    author_id: id
  }
  if (req.file && req.file.filename) {
    // 编辑文章时，重新上传文件了
    obj.cover_img = '/uploads/' + req.file.filename
  }
  let ret = await db.operateData(sql, [obj, params.id])
  // 响应状态
  if (ret && ret.affectedRows > 0) {
    res.json({
      status: 0,
      message: '编辑文章列表成功'
    })
  } else {
    res.json({
      status: 1,
      message: '编辑文章列表失败'
    })
  }
})

// 添加文章
router.post('/article/add', upload.single('cover_img'), async (req, res) => {
  // 1、获取文章表单数据
  let params = req.body
  let id = req.user.id
  // 2、把数据插入数据库
  let sql = 'insert into article set ?'
  let ret = await db.operateData(sql, {
    title: params.title,
    cate_id: params.cate_id,
    content: params.content,
    // 上传文件成功后，文件在服务器中的路径
    cover_img: '/uploads/' + req.file.filename,
    state: params.state,
    is_delete: 1,
    pub_date: new Date(),
    author_id: id
  })
  // 响应状态
  if (ret && ret.affectedRows > 0) {
    res.json({
      status: 0,
      message: '添加文章列表成功'
    })
  } else {
    res.json({
      status: 1,
      message: '添加文章列表失败'
    })
  }
})

// 分页查询文章列表数据
router.get('/article/list', async (req, res) => {
  // 1、获取请求参数
  let params = req.query
  
  // 查询参数处理：分类id和文章的状态
  let condition = ''
  if (params.cate_id) {
    condition += 'cate_id=' + params.cate_id
  }
  if (params.state) {
    if (condition.length > 0) {
      // 判断前面的参数state_id是否存在
      condition += ' and '
    }
    condition += 'state = "' + params.state + '"'
  }
  // cate_id = 1 and state = "已发布"
  
  // 处理分页参数：pagenum 表示页码； pagesize 表示每页多少条数据
  // limit 第一个参数表示从第几条开始查询：每页的条数 * (页码 - 1)
  let page = ' limit ' + params.pagesize * (params.pagenum - 1) + ', ' + params.pagesize
  // page = ' limit 5, 2'

  // 2、操作数据库查询数据
  // 2-1、查询文章列表数据
  let sql = 'select a.id, a.title, a.pub_date, a.state, c.name as cate_name from article as a join cate as c on a.cate_id = c.id'
  if (condition.length > 0) {
    sql += ' where ' + condition
  }
  if (page.length > 0) {
    sql += page
  }
  console.log(sql)
  let ret = await db.operateData(sql)
  // 2-2、查询文章列表总数
  let csql = 'select count(a.id) as total from article as a join cate as c on a.cate_id = c.id'
  if (condition.length > 0) {
    csql += ' where ' + condition
  }
  let cret = await db.operateData(csql)
  if (ret && ret.length > 0 && cret && cret.length > 0) {
    res.json({
      status: 0,
      message: '查询文章列表成功',
      data: ret,
      total: cret[0].total
    })
  } else {
    res.json({
      status: 1,
      message: '查询文章列表失败'
    })
  }
})

// 删除文章
router.get('/article/delete/:id', async (req, res) => {
  // 1、获取文章的id
  let id = req.params.id
  // 2、操作数据库
  let sql = 'delete from article where id = ?'
  let ret = await db.operateData(sql, id)
  // 3、响应结果
  if (ret && ret.affectedRows > 0) {
    res.json({
      status: 0,
      message: '删除文章成功'
    })
  } else {
    res.json({
      status: 1,
      message: '删除文章失败'
    })
  }
})

module.exports = router